﻿using System.Linq.Expressions;
using blog.Entity;
using blog.Model.Dto;
using blog.Model.Oo;
using blog.Model.Vo;
using Microsoft.EntityFrameworkCore;

namespace blog.Service;

public class GroupService :IGroupService
{
    
    BlogContext ef;

    public GroupService(BlogContext ctx)
    {
        ef = ctx;
    }
    public async Task<AjaxResult> Select(ListModel param,SearchKey key)
    {
        try
        {
            int page = param.page;
            int limit = param.limit;
            int offset = (page - 1) * limit;
            dynamic li;
            Expression<Func<SysGroup, bool>> expression = ExpressionExtension.True<SysGroup>();
            expression = expression.And(x => x.is_delete == 0);
            if (limit == -1)
            {
                li = await ef.SysGroups.Where(expression)
                    .ToListAsync();
            }
            else
            {
                li = await ef.SysGroups.Where(expression)
                    .Skip(offset)
                    .Take(limit)
                    .ToListAsync();
            }

            int count = await ef.SysGroups.Where(expression).CountAsync();
            return AjaxResult.Success(li,count);
        }
        catch (Exception ex)
        {
            return AjaxResult.Error(ex.Message);
        }
    }

    public async Task<AjaxResult> Delete(List<long> id)
    {
        throw new NotImplementedException();
    }

    public async Task<AjaxResult> Update(SysGroup param)
    {
        throw new NotImplementedException();
    }

    public async Task<AjaxResult> Insert(SysGroup param)
    {
        throw new NotImplementedException();
    }

    public async Task<AjaxResult> Find(long id)
    {
        throw new NotImplementedException();
    }
}